﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Selectors
{
    public partial class SelectDataElements : DialogBase, IDialogBase
    {

        private SelectDataElementTypes m_DataElementType;

        public SelectDataElements(SelectDataElementTypes type)
        {
            InitializeComponent();
            m_DataElementType = type;

        }

        public enum SelectDataElementTypes
        { 
            Assets,
            Projects
        }


        public int SelectedId { get; set; }
        public int SelectedProjectType { get; set; }
        public Common.Enumerations.DataTypes SelectedType { get; set; }
        public string SelectedName { get; set; }

        #region Asynch Processing (Implements IDialogBase)

        public override void DoAsynchWork(DoWorkEventArgs e)
        {
            //if (SelectedProjectType == Common.Constants.ProjectTypes.XmissionInspection)
            //{
            //    e.Result = PolePosition.v3.ServiceProxy.UtilityService.GetXmissionInspectionProjectData(SelectedProjectId);
            //    return;
            //}
            
            //if (SelectedProjectType == Common.Constants.ProjectTypes.XmissionGpsVisit)
            //{
            //    //e.Result = PolePosition.v3.ServiceProxy.UtilityService.GetXmissionGpsProjectData(SelectedProjectId, 100, 1, );
            //    return;
            //}
            
            //if (SelectedProjectType == Common.Constants.ProjectTypes.DistributionInspection)
            //{
            //    e.Result = PolePosition.v3.ServiceProxy.UtilityService.GetDistributionInspectionProjectData(SelectedProjectId);
            //    return;
            //}
        }

        public override void AsychWorkComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            //base.AsychWorkComplete(sender, e);
            ////do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            //if (e.Cancelled)
            //{
            //    this.DialogResult = DialogResult.Cancel;
            //    return;
            //}

            //if (e.Error != null || e.Result == null)
            //{
            //    //todo: create some sort of error dialog and use it here instead of messagebx
            //    DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

            ////if (SelectedProjectType == Common.Constants.ProjectTypes.XmissionInspection)
            ////{
            ////    //this.ResultData = e.Result as List<ProjectCommon2.Interfaces.ICustomEntity>;
            ////}
            ////else if (SelectedProjectType == Common.Constants.ProjectTypes.XmissionGpsVisit)
            ////{
            ////    this.ResultData = (e.Result as List<EntityLibrary.VendorVisitXMissionStructureEntity>).ToList<ProjectCommon2.Interfaces.ICustomEntity>();
            ////}
            ////else if (SelectedProjectType == Common.Constants.ProjectTypes.DistributionInspection)
            ////{
            ////    //this.ResultData = e.Result as List<ProjectCommon2.Interfaces.ICustomEntity>;
            ////}

            //this.ResultData = e.Result;
            //this.DialogResult = DialogResult.OK;
        }

        #endregion

        private void SelectDataElements_Load(object sender, EventArgs e)
        {
            projects1.SelectedProjects = null;

            switch (m_DataElementType)
            {
                case SelectDataElementTypes.Assets:
                    assetTypes1.Dock = DockStyle.Fill;
                    assetTypes1.BringToFront();
                    assetTypes1.ListEnabled = true;
                    buttonItemProject.Visible = false;
                    break;
                case SelectDataElementTypes.Projects:
                    projects1.Dock = DockStyle.Fill;
                    projects1.BringToFront();
                    buttonItemAsset.Visible = false;
                    break;
                default:
                    break;
            }
        
        }

        private void buttonXOk_Click(object sender, EventArgs e)
        {

            switch (m_DataElementType)
            {
                case SelectDataElementTypes.Assets:
                    if (assetTypes1.SelectedAssets == null ||
                        assetTypes1.SelectedAssets.Count() == 0)
                    {
                        DevComponents.DotNetBar.MessageBoxEx.Show("Please select an asset type", "Asset Type Required", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }

                    switch (assetTypes1.SelectedAssets[0])
                    {
                        case PolePosition.v3.Common.Enumerations.AssetTypes.Transmission:
                            SelectedType = Common.Enumerations.DataTypes.AssetsXmission;
                            break;
                        case PolePosition.v3.Common.Enumerations.AssetTypes.Distribution:
                            SelectedType = Common.Enumerations.DataTypes.AssetsDistribution;
                            break;
                        case PolePosition.v3.Common.Enumerations.AssetTypes.Meters:
                            SelectedType = Common.Enumerations.DataTypes.AssetsMeters;
                            break;
                        case PolePosition.v3.Common.Enumerations.AssetTypes.Transformers:
                            SelectedType = Common.Enumerations.DataTypes.AssetsTransformers;
                            break;
                        default:
                            break;
                    }

                    SelectedName = assetTypes1.SelectedAssets[0].ToString();

                    break;
                case SelectDataElementTypes.Projects:
                    if (projects1.SelectedProjects == null ||
                         projects1.SelectedProjects.Count() == 0)
                    {
                        DevComponents.DotNetBar.MessageBoxEx.Show("Please select a project", "Project Required", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        return;
                    }

                    SelectedId = projects1.SelectedProjects[0].Vendor_ProjectId.Value;


                    if (projects1.SelectedProjects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionGpsVisit)
                        SelectedType = Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit;
                    else if (projects1.SelectedProjects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionInspection)
                        SelectedType = Common.Enumerations.DataTypes.ProjectsXmissionInspection;
                    else if (projects1.SelectedProjects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.DistributionInspection)
                        SelectedType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;

                    SelectedName = projects1.SelectedProjects[0].Name;

                    break;
                default:
                    break;
            }


            this.DialogResult = DialogResult.OK;
        }
    }
}
